package com.example.lxj.system.controller;

import com.example.lxj.base.annoation.NoNeedLogin;
import com.example.lxj.base.annoation.Permission;
import com.example.lxj.base.controller.BaseController;
import com.example.lxj.base.entity.AjaxRes;
import com.example.lxj.base.entity.PageDTO;
import com.example.lxj.base.entity.TableDataInfo;
import com.example.lxj.system.entity.SysUser;
import com.example.lxj.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * 用户信息Controller
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户信息列表
     */
    @Permission(value = "system:user:list")
    @GetMapping("/list")
    public TableDataInfo list(SysUser sysUser, PageDTO pageDTO) {
        startPage(Optional.ofNullable(pageDTO.getPageNo()).orElse(1), Optional.ofNullable(pageDTO.getPageSize()).orElse(10));
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        return getDataTable(list);
    }

    /**
     * 获取用户信息详细信息
     */
    @Permission(value = "system:user:query")
    @GetMapping(value = "/{userId}")
    public AjaxRes getInfo(@PathVariable("userId") Long userId) {
        return success(sysUserService.selectSysUserByUserId(userId));
    }

    /**
     * 新增用户信息
     */
    @Permission(value = "system:user:add")
    @PostMapping("/add")
    public AjaxRes add(@RequestBody SysUser sysUser) {
        return toAjax(sysUserService.insertSysUser(sysUser));
    }

    /**
     * 修改用户信息
     */
    @Permission(value = "system:user:edit")
    @PutMapping
    public AjaxRes edit(@RequestBody SysUser sysUser) {
        return toAjax(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户信息
     */
    @Permission(value = "system:user:remove")
    @DeleteMapping("/{userId}")
    public AjaxRes remove(@PathVariable("userId") Long userId) {
        return toAjax(sysUserService.deleteSysUserByUserId(userId));
    }

    /**
     * 重置密码
     * @param sysUser
     * @return
     */
    @PostMapping("/resetPwd")
    public AjaxRes resetPwd(@RequestBody SysUser sysUser){
        return toAjax(sysUserService.resetSysUserPwd(sysUser));
    }

    /**
     * 注册用户
     * @param sysUser
     * @return
     */
    @PostMapping(value = "/register")
    @NoNeedLogin
    public AjaxRes register(@RequestBody SysUser sysUser){
        return toAjax(sysUserService.registerCustomerUser(sysUser));
    }
}
